package cn.darkfire.web.Controller;


import cn.darkfire.web.Service.UserService;
import cn.darkfire.web.Service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "del.do")
public class DelController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if(req.getParameter("username").length()>3){
            //its ok
            UserService userService = new UserServiceImpl();
            if(userService.removeUser(req.getParameter("username")).getCode()==200){
                req.getSession().setAttribute("success","删除用户成功！");
                req.getSession().removeAttribute("user"); //删除用户登录
                resp.sendRedirect("./login.jsp");
            }else{
                req.getSession().setAttribute("error","删除用户失败！");
                resp.sendRedirect("./index.jsp");
            }
        }else{
            req.getSession().setAttribute("error","删除用户失败！用户名参数错误！");
            resp.sendRedirect("./index.jsp");
        }
    }
}
